#include <msp430.h>
#include "main.h"

void setHeaterPwm()
{
	unsigned int base_voltage = 0;
	volatile int x = 0; //Declared volatile to force code inclusion, it's a hack.

	ADC10CTL0 |= REFON; //Enable the reference and let it settle.
	for(x = 0; x < 1024; x++)
	{}

	__bic_SR_register(GIE); //Disable interrupts...
	P1OUT |= HEATER_PWM; //Enable the heater to sample a reading...
	sampleChannel(&base_voltage, INCH_7);
	P1OUT &= ~(HEATER_PWM); //Disable heater
	ADC10CTL0 &= ~(REFON); //Disable reference.
	__bis_SR_register(GIE); //Disable interrupts...

	//Do the math to determine the necessary PWM.
	if(base_voltage > 0)
	{
		//pwm = (target * 100) * 1023 * resolution / base_voltage
		//pwm = (.9 * 100) * 1023 * 256 / base_voltage
		pwm_out[6] = HEATER_TARGET / base_voltage;
		pwm_out[6] &= 0x7F; //Having issues with blowing up heaters... this is very very bad...
	}
}

